<?php
/**
 * 钉钉登录插件 - 切换启用状态
 */

header('Content-Type: application/json; charset=utf-8');

// 获取 JSON 数据
$input = file_get_contents('php://input');
$data = json_decode($input, true);

$enabled = $data['enabled'] ?? false;

// 读取现有配置
$configFile = __DIR__ . '/../../config.php';
$config = [];
if (file_exists($configFile)) {
    $config = include $configFile;
}

// 更新启用状态
if (!isset($config['plugins'])) {
    $config['plugins'] = [];
}

if (!isset($config['plugins']['dingtalk'])) {
    $config['plugins']['dingtalk'] = [
        'enabled' => false,
        'app_key' => '',
        'app_secret' => '',
        'corp_id' => '',
        'agent_id' => '',
        'redirect_uri' => ''
    ];
}

$config['plugins']['dingtalk']['enabled'] = $enabled;

// 保存配置
$configContent = "<?php\n";
$configContent .= "/**\n";
$configContent .= " * 氚云API管理系统配置文件\n";
$configContent .= " * Alyr.net 企业级配置管理\n";
$configContent .= " */\n\n";
$configContent .= "return " . var_export($config, true) . ";\n";

if (file_put_contents($configFile, $configContent)) {
    echo json_encode([
        'success' => true,
        'message' => $enabled ? '钉钉登录已启用' : '钉钉登录已禁用'
    ]);
} else {
    echo json_encode([
        'success' => false,
        'message' => '状态更新失败，请检查文件权限'
    ]);
}
